
#ifndef STUDIO_ENCODE_H
#define STUDIO_ENCODE_H

#include "studio_macros.h"

#ifdef _WIN32
#include <windows.h>

class studio_char_conv
{
  public:
    /// <summary>
    /// 宽字符转窄字符
    /// </summary>
    /// <param name="wideStr"></param>
    /// <param name="str"></param>
    /// <returns></returns>
    static bool WcharToChar(const wchar_t* wideStr, std::string& str);

    /// <summary>
    /// 窄字符转宽字符
    /// </summary>
    /// <param name="multiByteStr"></param>
    /// <param name="wstr"></param>
    /// <returns></returns>
    static bool CharToWchar(const char* multiByteStr, std::wstring& wstr);

    /// <summary>
    /// 宽字符转窄字符
    /// 注意 ：需要手动释放内存, delete[] wideStr;
    /// </summary>
    /// <param name="multiByteStr"></param>
    /// <param name="wideStr">宽字符指针, 需要手动释放 delete[] wideStr</param>
    /// <returns></returns>
    static bool CharToWchar(const char* multiByteStr, wchar_t*& wideStr);
};

#endif // _WIN32

class studio_encode
{
  public:
    enum class enum_encode
    {
        Unknown = 0,
        ANSI = 1,  //
        UTF16_LE = 2,
        UTF16_BE = 3,
        UTF8_BOM = 4,
        UTF8 = 5,
        Unicode = 6,
        Unicode_BE = 7,
        GBK = 8
    };

    static bool is_utf8(const std::string &str);

    static enum_encode system_encode();

    /// <summary>
    /// 检查文件编码
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    static enum_encode check_file_encode(const std::string &path);

    /// <summary>
    /// URL编码
    /// </summary>
    /// <param name="src"></param>
    /// <returns></returns>
    static std::string url_encode(const std::string &src);

    /// <summary>
    /// URL解码
    /// </summary>
    /// <param name="src"></param>
    /// <returns></returns>
    static std::string url_decode(const std::string &src);

    /// <summary>
    /// 使用 iconv做的编码转换
    /// </summary>
    /// <param name="from">源字符串编码</param>
    /// <param name="to">目标字符串编码</param>
    /// <param name="text">源字符串</param>
    /// <returns></returns>
    static bool iconv_convert(const std::string &from, const std::string &to, const std::string &text, std::string &ret);

    /// <summary>
    /// 一常用的编码转化
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    static std::string gbk_utf8(const std::string &text);
    static std::string utf8_gbk(const std::string &text);
    static std::string unicode_gbk(const std::string &text);
    static std::string unicode_utf8(const std::string &text);

    /// <summary>
    /// utf8转宽字符, 这个函数没有经过验证,且于区别平台的大端序和小端序
    /// </summary>
    /// <param name="text"></param>
    /// <returns></returns>
    static std::wstring utf8_wchar(const std::string &text);

    static enum_encode is_utf8(const char *data, size_t size);

    static enum_encode detect_encode(const char *data, size_t size);

    static bool check_text_utf8(const char *str, int length);

    static std::string to_hex(const unsigned char *s_byte, const size_t &len);
    static void from_hex(const char *str, unsigned char *Hstr);

    static std::wstring cxx11_string_wstring(const std::string &str);

    static std::string cxx11_wstring_string(const std::wstring &wstr);
};

#endif  // STUDIO_ENCODE_H
